package com.ethanzzh.security.core.validate.sms;

import com.ethanzzh.security.core.properties.SecurityProperties;
import com.github.qcloudsms.SmsSingleSender;
import com.github.qcloudsms.SmsSingleSenderResult;
import org.json.JSONException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import javax.xml.ws.http.HTTPException;
import java.io.IOException;

public class DefaultSmsSender implements SmsCodeSender {

    private static final Logger LOGGER = LoggerFactory.getLogger(DefaultSmsSender.class);

    @Autowired
    private SecurityProperties securityProperties;

    @Override
    public void send(String mobile, String code) {

        String[] params = {code, "5"};//参数，验证码为smsCode，5分钟内填写
        String sign = "ethanzzh"; //这个是签名内容  在申请的时候填的
        try {
            SmsSingleSender smsSingleSender = new SmsSingleSender(securityProperties.getCode().getSms().getSmsAppid(),securityProperties.getCode().getSms().getSmsKey() );
            SmsSingleSenderResult result = smsSingleSender.sendWithParam("86",
                    mobile, securityProperties.getCode().getSms().getTpl_id(), params,sign,"","");
            LOGGER.info(result.toString());

        } catch (HTTPException e) {
            // HTTP响应码错误
            e.printStackTrace();

        } catch (JSONException e) {
            // json解析错误
            e.printStackTrace();
        } catch (IOException e) {
            // 网络IO错误
            e.printStackTrace();
        } catch (Exception e) {
            // 网络IO错误
            e.printStackTrace();
        }
    }
}
